package br.com.projeto.activity;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.projeto.MegaSenaApplication;
import br.com.projeto.adapter.ListarJogosAdapter;
import br.com.projeto.controller.NumerosAleatorios;
import br.com.projeto.model.Jogo;
import br.com.projeto.task.ListarJogosTask;
import br.com.projeto.task.SalvarJogosTask;

import com.tbm.megasena.R;

public class GerarNumerosActivity extends BaseActivity {
	
	private TextView txtNumber1, txtNumber2, txtNumber3, txtNumber4, txtNumber5, txtNumber6;
	private ListView lvGames;
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.gerar_list);
		
		handler = new Handler(this);
		
		txtNumber1 = (TextView) findViewById(R.id.num1);
		txtNumber2 = (TextView) findViewById(R.id.num2);
		txtNumber3 = (TextView) findViewById(R.id.num3);
		txtNumber4 = (TextView) findViewById(R.id.num4);
		txtNumber5 = (TextView) findViewById(R.id.num5);
		txtNumber6 = (TextView) findViewById(R.id.num6);
		
		lvGames = (ListView) findViewById(R.id.list);
		
		Button btSalvar = (Button) findViewById(R.id.salvarJogo);
		btSalvar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showProgressDialog(getString(R.string.saving));
				new SalvarJogosTask(handler).execute(atribuirCampos());
			}
		});
		
		Button btGerar = (Button) findViewById(R.id.gerarNumeros);
		btGerar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listarNumerosAleatorios();
			}
		});
		
		this.listarNumerosAleatorios();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		showProgressDialog(getString(R.string.loading));
		new ListarJogosTask(handler).execute();
	}
	
	private Jogo atribuirCampos() {
		Jogo jogo = new Jogo();
		jogo.setNumero1(txtNumber1.getText().toString());
		jogo.setNumero2(txtNumber2.getText().toString());
		jogo.setNumero3(txtNumber3.getText().toString());
		jogo.setNumero4(txtNumber4.getText().toString());
		jogo.setNumero5(txtNumber5.getText().toString());
		jogo.setNumero6(txtNumber6.getText().toString());
		
		return jogo;
	}

	private void listarNumerosAleatorios() {
		NumerosAleatorios numerosAleatorios = new NumerosAleatorios();
		
		List<Integer> lista = numerosAleatorios.sortearSequencia();	
		
		txtNumber1.setText(lista.get(0).toString());
		txtNumber2.setText(lista.get(1).toString());
		txtNumber3.setText(lista.get(2).toString());
		txtNumber4.setText(lista.get(3).toString());
		txtNumber5.setText(lista.get(4).toString());
		txtNumber6.setText(lista.get(5).toString());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		super.handleMessage(msg);
		switch (msg.what) {
		case MegaSenaApplication.HANDLE_LIST_OK:
			lvGames.setAdapter(new ListarJogosAdapter(this, (List<Jogo>) msg.obj, handler));
			break;
			
		case MegaSenaApplication.HANDLE_SAVE_OK:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogoOk), Toast.LENGTH_LONG).show();
			onResume();
			break;
			
		case MegaSenaApplication.HANDLE_SAVE_ERRO:
			Toast.makeText(getApplicationContext(), getText(R.string.lbl_jogoError), Toast.LENGTH_LONG).show();
			break;
		}
		return true;
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
}
